package org.agilelang.editor.syntaxtext;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.agilelang.editor.syntaxtext.dialogpopup.DlgApplicationDB;
import org.agilelang.editor.syntaxtext.dialogpopup.DlgApplicationDefine;
import org.agilelang.editor.syntaxtext.dialogpopup.DlgControllerDefine;
import org.agilelang.editor.syntaxtext.dialogpopup.DlgMenuDefine;
import org.agilelang.editor.syntaxtext.dialogpopup.DlgModelDefine;
import org.agilelang.editor.syntaxtext.dialogpopup.DlgScaffoldDefine;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

public class PopupSyntaxText {

    public JPopupMenu popup;
    private RSyntaxTextArea editor;
    String path_source = "";
    String path_xml = "";
    //Project project;

    public PopupSyntaxText(RSyntaxTextArea editor) {
        this.editor = editor;

        init();
    }

    public JPopupMenu getPopup() {
        return popup;
    }

    public void init() {
        popup = new JPopupMenu();
        JMenu sm_app = new JMenu("Application");
        JMenuItem mi_define_app = new JMenuItem("define");
        mi_define_app.addActionListener(mi_define_app());
        JMenuItem mi_db_app = new JMenuItem("db");
        mi_db_app.addActionListener(mi_db_app());
        sm_app.add(mi_define_app);
        sm_app.add(mi_db_app);
        popup.add(sm_app);

        JMenu sm_controller = new JMenu("Controller");
        JMenuItem mi_define_controller = new JMenuItem("define");
        mi_define_controller.addActionListener(mi_define_controller());
        JMenuItem mi_function_controller = new JMenuItem("function");
        mi_function_controller.addActionListener(mi_function_controller());
        JMenuItem mi_view_controller = new JMenuItem("view");
        mi_view_controller.addActionListener(mi_view_controller());
        sm_controller.add(mi_define_controller);
        sm_controller.add(mi_function_controller);
        sm_controller.add(mi_view_controller);
        popup.add(sm_controller);

        JMenu sm_model = new JMenu("Model");
        JMenuItem mi_define_model = new JMenuItem("define");
        mi_define_model.addActionListener(mi_define_model());
        JMenuItem mi_attribute_model = new JMenuItem("attribute");
        mi_attribute_model.addActionListener(mi_attribute_model());
        JMenuItem mi_relation_model = new JMenuItem("relation");
        mi_relation_model.addActionListener(mi_relation_model());
        sm_model.add(mi_define_model);
        sm_model.add(mi_attribute_model);
        sm_model.add(mi_relation_model);
        popup.add(sm_model);

        JMenu sm_scaffold = new JMenu("Scaffold");
        JMenuItem mi_define_scaffold = new JMenuItem("define");
        mi_define_scaffold.addActionListener(mi_define_scaffold());
        JMenuItem mi_attribute_scaffold = new JMenuItem("attribute");
        mi_attribute_scaffold.addActionListener(mi_attribute_scaffold());
        JMenuItem mi_relation_scaffold = new JMenuItem("relation");
        mi_relation_scaffold.addActionListener(mi_relation_scaffold());
        JMenuItem mi_function_scaffold = new JMenuItem("function");
        mi_function_scaffold.addActionListener(mi_function_scaffold());
        JMenuItem mi_view_scaffold = new JMenuItem("view");
        mi_view_scaffold.addActionListener(mi_view_scaffold());
        sm_scaffold.add(mi_define_scaffold);
        sm_scaffold.add(mi_attribute_scaffold);
        sm_scaffold.add(mi_relation_scaffold);
        sm_scaffold.add(mi_function_scaffold);
        sm_scaffold.add(mi_view_scaffold);
        popup.add(sm_scaffold);

        JMenu sm_menu = new JMenu("Menu");
        JMenuItem mi_menu_menu = new JMenuItem("Menu");
        mi_menu_menu.addActionListener(mi_menu_menu());
        JMenuItem mi_item_menu = new JMenuItem("Item");
        mi_item_menu.addActionListener(mi_item_menu());
        sm_menu.add(mi_menu_menu);
        sm_menu.add(mi_item_menu);
        popup.add(sm_menu);
        popup.addSeparator();
    }

    private ActionListener mi_define_app() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                DlgApplicationDefine dlg = new DlgApplicationDefine();
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }

            }
        };
        return action;
    }

    private ActionListener mi_db_app() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                DlgApplicationDB dlg = new DlgApplicationDB();
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }
            }
        };
        return action;
    }

    private ActionListener mi_define_controller() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                DlgControllerDefine dlg = new DlgControllerDefine();
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }
            }
        };
        return action;
    }

    private ActionListener mi_function_controller() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*ViewFrame.saveDef();
                path_source = ViewFrame.app_path;
                scanner_parser();
                readProject();
                DlgControllerAddFunction dlg = new DlgControllerAddFunction();
                dlg.setNameControllers(getListController());
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }*/
            }
        };
        return action;
    }

    private ActionListener mi_view_controller() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*ViewFrame3.saveDef();
                path_source = ViewFrame3.app_path;
                scanner_parser();
                readProject();
                DlgControllerAddView dlg = new DlgControllerAddView();
                dlg.setNameControllers(getListController());
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }*/
            }
        };
        return action;
    }

    private ActionListener mi_define_model() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                DlgModelDefine dlg = new DlgModelDefine();
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }
            }
        };
        return action;
    }

    private ActionListener mi_attribute_model() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*ViewFrame3.saveDef();
                path_source = ViewFrame3.app_path;
                scanner_parser();
                readProject();
                DlgModelAddAttribute dlg = new DlgModelAddAttribute();
                dlg.setNameModels(getListModel());
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }*/
            }
        };
        return action;
    }

    private ActionListener mi_relation_model() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*ViewFrame3.saveDef();
                path_source = ViewFrame3.app_path;
                scanner_parser();
                readProject();
                DlgModelAddRelation dlg = new DlgModelAddRelation(project);
                //dlg.setNameModels(getListModel());
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }*/
            }
        };
        return action;
    }

    private ActionListener mi_define_scaffold() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                DlgScaffoldDefine dlg = new DlgScaffoldDefine();
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }
            }
        };
        return action;
    }

    private ActionListener mi_attribute_scaffold() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*ViewFrame3.saveDef();
                path_source = ViewFrame3.app_path;
                scanner_parser();
                readProject();
                DlgScaffoldAddAttribute dlg = new DlgScaffoldAddAttribute();
                dlg.setNameModels(getListModel());
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }*/
            }
        };
        return action;
    }

    private ActionListener mi_relation_scaffold() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*ViewFrame3.saveDef();
                path_source = ViewFrame3.app_path;
                scanner_parser();
                readProject();
                DlgScaffoldAddRelation dlg = new DlgScaffoldAddRelation(project);
                //dlg.setNameModels(getListModel());
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }*/
            }
        };
        return action;
    }

    private ActionListener mi_function_scaffold() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.append("");
            }
        };
        return action;
    }

    private ActionListener mi_view_scaffold() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.append("");
            }
        };
        return action;
    }

    private ActionListener mi_menu_menu() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                
                DlgMenuDefine dlg = new DlgMenuDefine();
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }
            }
        };
        return action;
    }

    private ActionListener mi_item_menu() {
        ActionListener action = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*ViewFrame3.saveDef();
                path_source = ViewFrame3.app_path;
                scanner_parser();
                readProject();
                DlgMenuAddMenuItem dlg = new DlgMenuAddMenuItem(project);
                //dlg.setNameModels(getListModel());
                dlg.setVisible(true);
                if (dlg.isStatus()) {
                    editor.append(dlg.getSentences());
                }*/
            }
        };
        return action;
    }

    /*private String[] getListController() {
        List<Controller> list = project.controllers;
        int t = list.size();
        String[] arry = new String[t];
        for (int i = 0; i < t; i++) {
            Controller controller = list.get(i);
            arry[i] = controller.name;
        }
        return arry;
    }*/

    /*private String[] getListModel() {
        List<Model> listm = project.models;
        //List<Scaffold> lists = project.scaffolds;
        int t = listm.size();
        //int v = lists.size();
        String[] arry = new String[t];// + v];
        for (int i = 0; i < t; i++) {
            Model model = listm.get(i);
            arry[i] = model.name;
        }

        return arry;
    }*/

    /*public void scanner_parser() {
        Scanner scaner = new Scanner();
        System.out.println("Scaner " + path_source);
        if (!scaner.process(path_source + File.separator + "def.agile")) {
            System.err.println(scaner.getError());
            return;
        }
        if (!scaner.process(path_source + File.separator + "controllers.agile")) {
            System.err.println(scaner.getError());
            return;
        }
        if (!scaner.process(path_source + File.separator + "models.agile")) {
            System.err.println(scaner.getError());
            return;
        }
        if (!scaner.process(path_source + File.separator + "scaffolds.agile")) {
            System.err.println(scaner.getError());
            return;
        }

        System.out.println("Parser...");
        Parser parser = new Parser();
        parser.process(scaner.getLines());
        try {
            path_xml = (new File(path_source)).getParentFile().getAbsolutePath() + "/metadef.xml";
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path_xml)));
            pw.print(parser.getXml());
            pw.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }*/

    /*public void readProject() {
        File file = new File(path_xml);
        if (!file.exists()) {
            System.err.println("Fichero XMl no existe");
            return;
        }
        //leer xml
        InputStream is = null;
        Reader reader = null;
        try {
            is = new FileInputStream(path_xml);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (Exception ex) {
            System.err.println("Error al leer fichero XML : " + ex);
        }
        //Poblar objecto Project        
        JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(Project.class);
            Unmarshaller u = jc.createUnmarshaller();
            project = (Project) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

    }*/
}
